/*
给定M×N矩阵，每一行、每一列都按升序排列，请编写代码找出某元素。

思路：从右上角开始判断(或者左下角)
 */

/**
 * @param {number[][]} matrix
 * @param {number} target
 * @return {boolean}
 */
var searchMatrix = function (matrix, target) {
  if (!matrix.length || !matrix[0].length) {
    return false
  }
  const M = matrix.length - 1
  const N = matrix[0].length - 1

  let rA = 0
  let cA = N
  while (rA <= M && cA <= N && rA >= 0 && cA >= 0) {
    if (target === matrix[rA][cA]) {
      return true
    } else if (target > matrix[rA][cA]) {
      rA++
    } else {
      cA--
    }
  }
  return false
}
